package dao;


import entity.Article;
import entity.User;
import util.DBTools;

import java.util.ArrayList;
import java.util.Date;

import java.sql.*;

public class ArticleDao {
    public static int addArticle(Integer uid, String title, String summary, String content, Integer wordTotal) {

        String sql = "insert into article(uid,title,summary,content,wordtotal,addtime) values(?,?,?,?,?,?)";
        int total = 0;
        Connection connection = DBTools.getConnection();
        PreparedStatement ps = null;

        try {
            ps = connection.prepareStatement(sql);
            ps.setInt(1, uid);
            ps.setString(2, title);
            ps.setString(3, summary);
            ps.setString(4, content);
            ps.setInt(5, wordTotal);
            Date now = new Date();
            Timestamp addtime = new Timestamp(now.getTime());
            ps.setTimestamp(6, addtime);

            total = ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBTools.close(connection, ps, null);
        }
        return total;
    }

    public ArrayList<Article> getArticles() {
        String sql = "SELECT article.id as aid,title,summary,content,looktotal,liketotal,commenttotal,wordtotal,article.`status` as astatus,article.addtime as atime,\n" +
                "user.id as uid,nickname,avatar\n" +
                "FROM article,`user` WHERE `user`.id=article.uid;";
        ArrayList<Article> articles = new ArrayList<>();
        Connection connection = DBTools.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connection.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {

                Article article = new Article();
                article.setId(rs.getInt("aid"));
                article.setTitle(rs.getString("title"));
                article.setSummary(rs.getString("summary"));
                article.setContent(rs.getString("content"));
                article.setLiketotal(rs.getInt("liketotal"));
                article.setLooktotal(rs.getInt("looktotal"));
                article.setCommenttotal(rs.getInt("commenttotal"));
                article.setStatus(rs.getInt("astatus"));
                article.setWordtotal(rs.getInt("wordtotal"));
                article.setAddtime(new Date(rs.getTimestamp("atime").getTime()));
                //将作者字段抽象作者对象
                User user = new User();
                user.setId(rs.getInt("uid"));
                user.setAvatar(rs.getString("avatar"));
                user.setNickname(rs.getString("nickname"));

                article.setUser(user);

                //将每行的文章对象添加到文章数组中
                articles.add(article);


            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBTools.close(connection, ps, rs);
        }


        return articles;
    }

    public static Article getArticleById(String id) {

        Article article = null;
        String sql = "SELECT article.id as aid,title,summary,content,looktotal,liketotal,commenttotal,wordtotal,article.`status` as astatus,article.addtime as atime,\n" +
                "user.id as uid,nickname,avatar\n" +
                "FROM article,`user` WHERE `user`.id=article.uid and article.id=?;";

        Connection connection = DBTools.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1,id);
            rs = ps.executeQuery();
            while (rs.next()) {

                article = new Article();
                article.setId(rs.getInt("aid"));
                article.setTitle(rs.getString("title"));
                article.setSummary(rs.getString("summary"));
                article.setContent(rs.getString("content"));
                article.setLiketotal(rs.getInt("liketotal"));
                article.setLooktotal(rs.getInt("looktotal"));
                article.setCommenttotal(rs.getInt("commenttotal"));
                article.setStatus(rs.getInt("astatus"));
                article.setWordtotal(rs.getInt("wordtotal"));
                article.setAddtime(new Date(rs.getTimestamp("atime").getTime()));
                //将作者字段抽象作者对象
                User user = new User();
                user.setId(rs.getInt("uid"));
                user.setAvatar(rs.getString("avatar"));
                user.setNickname(rs.getString("nickname"));

                article.setUser(user);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBTools.close(connection, ps, rs);
        }

        return article;

    }

    public static int deleteArticleById(String id) {
        int total = 0;

        String sql = "update article set status=1 where id = ?";

        Connection connection = DBTools.getConnection();
        PreparedStatement ps =null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1,id);
            total = ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBTools.close(connection,ps,null);
        }

        return total;

    }

    public int updateArticleById(String id, String title, String summary, String content) {
        int total = 0;
        String sql = "update article set title=?,summary=?,content=? where id=?";
        Connection connection = DBTools.getConnection();
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1,title);
            ps.setString(2,summary);
            ps.setString(3,content);
            ps.setString(4,id);

            total = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBTools.close(connection,ps,null);
        }
        return total;
    }

    public int peopleLike(String id) {
        int result = 0;

        String sql1 = "select liketotal from article where id = ?";

        String sql2 = "update article set liketotal = ? where id = ?";

        Connection connection = DBTools.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null ;
        try {
            ps = connection.prepareStatement(sql1);
            ps.setString(1, id);
            rs = ps.executeQuery();
            rs.next();
            int liketotal = rs.getInt("liketotal");

            ps = connection.prepareStatement(sql2);
            ps.setInt(1,liketotal + 1);
            ps.setString(2,id);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBTools.close(connection, ps, rs);
        }

        return result;
    }

    public int lookTotal(String id) {
        int result = 0;

        String sql1 = "select looktotal from article where id = ?";

        String sql2 = "update article set looktotal = ? where id = ?";

        Connection connection = DBTools.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null ;
        try {
            ps = connection.prepareStatement(sql1);
            ps.setString(1, id);
            rs = ps.executeQuery();
            rs.next();
            int looktotal = rs.getInt("looktotal");

            ps = connection.prepareStatement(sql2);
            ps.setInt(1,looktotal + 1);
            ps.setString(2,id);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBTools.close(connection, ps, rs);
        }

        return result;
    }

}
